﻿using ComponentFactory.Krypton.Toolkit;
using Microsoft.TeamFoundation.Client;
using Nini.Config;
using System;
using System.Net;
using System.Windows.Forms;

namespace TfsHelper.WinFrontEnd
{
    static class Program
    {
        public static IConfigSource config = new XmlConfigSource(Application.StartupPath + "\\Config.xml");
        public static string _myUri;
        public static TeamFoundationServer tfsSrv = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            frmTfsUrl url = new frmTfsUrl();
            KryptonManager kryptonMgr = new KryptonManager();
            kryptonMgr.GlobalPalette = new PaletteOffice2007Black();

            _myUri = config.Configs["General"].Get("TfsUrl");
            while (string.IsNullOrWhiteSpace(_myUri))
            {
                if (string.IsNullOrWhiteSpace(_myUri))
                {
                    if (url.ShowDialog() == DialogResult.Cancel)
                    {
                        KryptonMessageBox.Show("You need to set the TFS Url in order to use the tool.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        _myUri = config.Configs["General"].Get("TfsUrl");
                    }
                }
            }

            tfsSrv = new TeamFoundationServer(_myUri, new NetworkCredential());

            frmLogin login = new frmLogin(tfsSrv, _myUri);
            login.ShowDialog();

            if (tfsSrv.HasAuthenticated)
                Application.Run(new frmMain());
            else
                KryptonMessageBox.Show("You need to login in order to use the tool.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
